package com.ds;


import javax.xml.bind.annotation.*;
import javax.swing.tree.TreeNode;
import java.util.*;
import java.util.logging.Logger;
import java.io.*;

/**
 * Created by IntelliJ IDEA.
 * User: daniel
 * Date: 28/10/2009
 * Time: 16:15:42
 */
@XmlRootElement( name = "tvshows" )
public class Shows implements TreeNode {

    private static final int MAX_RETRIES = 5;
    private static final long WAIT_AFTER_FAILURE = 3000;
    private static Logger logger = Logger.getLogger("rsstorrent");

    @XmlElement( name = "show" )
    private List<TVShow> shows = new ArrayList<TVShow>();

    @XmlElement( name = "myEpisodesRSS")
    private String myEpisodesRSSLink;

    public void add(TVShow show){
        shows.add(show);
    }

    public String toString() {
        return "TV Shows";
    }

    public String getMyEpisodesRSSLink() {
        return myEpisodesRSSLink;
    }

    public void readRSSFeed(boolean forceRead) {
        for (TVShow tvShow : shows) {
            int failCount = 0;
            boolean failed = false;
            do {
                try {
                    if(forceRead || !tvShow.isReadOK()) {
                        tvShow.readFeed();
                        tvShow.setReadOK(true);
                        failed = false;
                        failCount = 0;
                        try {
                            Thread.sleep(1000);
                        } catch (InterruptedException e) {
                        }
                    }
                } catch (Exception e) {
                    failCount++;
                    failed = true;
                    logger.info("Shows.readRSSFeed : failed reading xml of " + tvShow.retrieveName() + ". Error: " + e.getMessage());

                    final Writer eResult = new StringWriter();
                    final PrintWriter printWriter = new PrintWriter(eResult);
                    e.printStackTrace(printWriter);
                    logger.severe(eResult.toString());

                    try {
                        Thread.sleep(WAIT_AFTER_FAILURE);
                    } catch (InterruptedException se) {
                    }

                    //e.printStackTrace();
                }

            }while(failed && failCount<MAX_RETRIES);
        }
        //link all tree nodes
        setParentChildLink();
    }

    public void setParentChildLink() {
        for (TVShow show : shows){
            show.setShows(this);
        }
    }


    //TreeNode implementation

    public TreeNode getChildAt(int childIndex) {
        return shows.get(childIndex);
    }

    public int getChildCount() {
        return shows.size();
    }

    public TreeNode getParent() {
        return null;
    }

    public int getIndex(TreeNode node) {
        //noinspection SuspiciousMethodCalls
        return shows.indexOf(node);
    }

    public boolean getAllowsChildren() {
        return true;
    }

    public boolean isLeaf() {
        return false;
    }

    public Enumeration children() {
        return Collections.enumeration(shows);
    }

}
